﻿function Cname()
{
 var Name=document.getElementById("Tname").value;

 if(Name.length=="")
 {
  document.getElementById("Rname").style.color="red";
  document.getElementById("Rname").innerHTML=" 请输用户名";
  return false;
 }
 else if(Name.length<6 || Name.length>16)
 {
  document.getElementById("Rname").style.color="red";
  document.getElementById("Rname").innerHTML=" 6-16位字符";
  return false;
 }
 else
 {
  document.getElementById("Rname").style.color="green";
  document.getElementById("Rname").innerHTML=" 用户名通过";
  return true;
 }
}
function Cpwd1()
{
 var pwd1=document.getElementById("Tpwd1").value;
 if(pwd1.length=="")
 {
  document.getElementById("Rpwd1").style.color="red";
  document.getElementById("Rpwd1").innerHTML=" 密码不能为空";
  return false;
 }
 else if(pwd1.length<6 || pwd1.length>16)
 {
  document.getElementById("Rpwd1").style.color="red";
  document.getElementById("Rpwd1").innerHTML=" 6-16位字符";
  return false;
 }
 else
 {
  document.getElementById("Rpwd1").style.color="green";
  document.getElementById("Rpwd1").innerHTML=" 密码通过";
  return true;
 }
}
function Cpwd2()
{
 var pwd1=document.getElementById("Tpwd1").value;
 var pwd2=document.getElementById("Tpwd2").value;
 if(pwd2!=pwd1)
 {
  document.getElementById("Rpwd2").style.color="red";
  document.getElementById("Rpwd2").innerHTML=" 密码保持一致";
  return false;
 }
 else
 {
  document.getElementById("Rpwd2").style.color="green";
  document.getElementById("Rpwd2").innerHTML=" 验证密码通过";
  return true;
 }
}
function Cemail()
{
 var email=document.getElementById("Temail").value;
 if(email.length=="")
 {
  document.getElementById("Remail").style.color="red";
  document.getElementById("Remail").innerHTML=" 请输入Email";
  return false;
 }
 else if(email.indexOf("@")<1 || email.indexOf(".")<2 || email.indexOf(".")<email.indexOf("@"))
 {
  document.getElementById("Remail").style.color="red";
  document.getElementById("Remail").innerHTML=" Email不合法";
  return false;
 }
 else
 {
 // document.getElementById("Remail").style.color="green";
  //document.getElementById("Remail").innerHTML="  格式合法";
 
 }
 
 $.ajax({
		url:"userAction!checkUser",
		type:"post",
		async:false,
		data:{'email':email},
		success:function(result,status){
		//	alert("result:"+result);
			var objs = eval("("+result+")");
			
			
			if(objs['errorMsg'].length!=0){
				 document.getElementById("Remail").style.color="red";
				  document.getElementById("Remail").innerHTML=" 邮箱已被注册！";
				  return false;
			}else
			{
				 document.getElementById("Remail").style.color="green";
				  document.getElementById("Remail").innerHTML=" 邮箱可使用";
				  return true;
			}			
		}		
	});
 return true;
 
}


function Ctel()
{
 var Tel=document.getElementById("Ttel").value;
 for(var i=0;i<Tel.length;i++)
 {
  if(Tel.charAt(i)<"0" || Tel.charAt(i)>"9")
  {
   document.getElementById("Rtel").style.color="red";
   document.getElementById("Rtel").innerHTML=" 只能是11位数字";
   return false;
  }
 }
 if(Tel.length=="")
 {
  document.getElementById("Rtel").style.color="red";
  document.getElementById("Rtel").innerHTML=" 请输入手机号码";
  return false;
 }
 else if(Tel.length<0 || Tel.length>11)
 {
  document.getElementById("Rtel").style.color="red";
  document.getElementById("Rtel").innerHTML=" 只能是11位数字";
  return false;
 }
 else
 {
  document.getElementById("Rtel").style.color="green";
  document.getElementById("Rtel").innerHTML=" 格式合法";
  return true;
 }
}

function Cqq()
{
 var qq=document.getElementById("qq").value;
 for(var i=0;i<qq.length;i++)
 {
  if(qq.charAt(i)<"0" || qq.charAt(i)>"9")
  {
   document.getElementById("Rqq").style.color="red";
   document.getElementById("Rqq").innerHTML=" 只能是数字";
   return false;
  }else
  {
	  document.getElementById("Rqq").style.color="green";
	  document.getElementById("Rqq").innerHTML=" 格式合法";
	  return true;
	 }
 }
}
  

function registSubmit()
{
 if(Cname() && Cpwd1() && Cpwd2() && Cemail() && Ctel())
 {
	 var name=document.getElementById("Tname").value;
	 var password=document.getElementById("Tpwd1").value;
	 var rePassword=document.getElementById("Tpwd2").value;
	 var email=document.getElementById("Temail").value;
	 var phone=document.getElementById("Ttel").value;
	 var qq=document.getElementById("qq").value;
	 
	 $.ajax({
			url:"userAction!register",
			type:"post",
			async:false,
			data:{'email':email,'password':password,"name":name,"rePassword":rePassword,"phone":phone,"qq":qq},
			success:function(result,status){
			//	alert("result:"+result);
				var objs = eval("("+result+")");
				
				if(objs['errorMsg'].length!=0){
					alert("注册失败："+objs['errorMsg']);
				}else
				{
					alert("注册成功！快去登录吧~");
					location.href="login.jsp";
				}
				
			}
			
		});
 }
 else
 {
  alert("资料填写不完整，请填写完整！");
 }
}